﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormsAppToPDF
{
    public partial class FormMain : Form
    {

        [DllImport("kernel32.dll")]
        public static extern IntPtr _lopen(string lpPathName, int iReadWrite);

        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr hObject);

        public const int OF_READWRITE = 2;
        public const int OF_SHARE_DENY_NONE = 0x40;
        public readonly IntPtr HFILE_ERROR = new IntPtr(-1);


        string path = "";
        string newpath = "";
        string newpath1 = "";

        public FormMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            IniFiles.ClearLog();
            if (File.Exists(@"Logo.png"))
            {
                pictureBox1.Image = Image.FromFile(@"Logo.png");
            }
            //btnCreCert.Enabled = false;
            //btnCreCert.Visible = false;
            btnToPDF.Visible = false;
            textBox1.Visible = false;
            textBox2.Visible = false;
            textBox3.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (fBDialog.ShowDialog() == DialogResult.OK)
            {
                path=fBDialog.RootFolder.ToString();
            }
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            DateTime dt = DateTime.Now;
            string d = string.Format("{0:yyyyMMddHHmmssffff}", dt);
            //textBox1.Text=(string)e.Data.GetData(DataFormats.FileDrop).GetType().Name.ToString();
            path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            if (!path.EndsWith("pdf"))
            {
                MessageBox.Show("注意文件不是PDF文件");
            }
            else
            {
                textBox1.Text = path;
                newpath = Path.GetDirectoryName(path) + "\\" + Path.GetFileNameWithoutExtension(path) + "_受控" + Path.GetExtension(path);
                if (PDFHelper.AddImageWatermarkPDF(path, newpath, @"Water.png", 10, 10))
                {
                    textBox2.Text = newpath;
                }
                newpath1 = Path.GetDirectoryName(path) + "\\" + Path.GetFileNameWithoutExtension(path) + "_受控_Sign" + Path.GetExtension(path);
                CreateSign.Sign(path, newpath1);
               
            }
        }
        

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {            
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                //e.Effect = DragDropEffects.Copy;
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect=DragDropEffects.None;
            }            
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            //CreatPublicSeal.CreatSeal("受高空", "", "d:\\");
            CreatPublicSeals.CreatSeal("受控", "", "d:\\");
        }

        private void btnCreCert_Click(object sender, EventArgs e)
        {
            //Cert.Create();
            FormCert cert = new FormCert();
            cert.ShowDialog();  
        }
    }
}
